使用 Terraform 在 GCP 上建立外部和內部的全球 IP 地址


Posted by sz9751210 on 2024-01-09

👨‍💻簡介

這篇文章將會說明如何快速在 Google Cloud Platform 上使用 Terraform 建立外部與內部的全球 IP 。

前提條件

  • Google Cloud Platform (GCP) 帳號: 確保有一個有效的 GCP 帳號。
  • 安裝Terraform: 還沒安裝可以參考 Terraform 安裝指南
  • 基礎 Terraform 知識: 瞭解基本的 Terraform 命令和概念,如果需要,可參考 Terraform 入門指南
    ## 🎯setup

步驟 1:建立 provider.tf

定義GCP Provider 和所需的 Terraform 版本。

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "5.7.0"
    }
  }
}

provider "google" {
  project = "project-id"
  region  = "asia-east1"
}

步驟 2:建立 main.tf

在此檔案中定義全球 IP 地址資源。

  • External IP
    resource "google_compute_global_address" "default" {
    name          = "global-external-ip"
    address_type  = "EXTERNAL"
    ip_version    = "IPV4"
    labels = {
      "dept" = "devops",
      "env"  = "dev",
      "type" = "internal"
    }
    }
    
  • Internal IP
    resource "google_compute_global_address" "default" {
    name          = "global-internal-ip"
    address_type  = "INTERNAL"
    network       = "default"
    ip_version    = "IPV4"
    purpose       = "PRIVATE_SERVICE_CONNECT"
    address       = "100.100.100.105"
    labels = {
      "dept" = "devops",
      "env"  = "dev",
      "type" = "external"
    }
    }
    
    參數介紹
  • name:ip name
  • address_type:建立的網路類型,分為內網 (Internal) 以及外網 (External)
  • network:使用的虛擬私有網路
  • ip_version:IP 版本,指定 IP 地址使用的協議版本,如 IPv4 或 IPv6。
  • purpose:對於內部 IP,此參數定義了 IP 地址的使用目的,例如 PRIVATE_SERVICE_CONNECT 表示用於私有服務連接。
  • address:指定內網 IP。
  • labels:使用的 label,有助於資源分類和管理。

步驟 3:初始化 Terraform

terraform init

步驟 4:執行 Dry Run

terraform plan

步驟 5:部署

terraform apply

資源刪除

部署完成後,如果需要清理資源,可執行:

terraform destroy

🛠️ 將 Global IP Addresses Terraform 設定檔模組化

首先,我們要把 main.tf 改成用變數來替代掉原本寫死的值。這樣做設定起來更靈活,也更好管理。

1. 改造 main.tf 讓它更模組化

首先,我們要把 main.tf 改一改,用變數來替代掉原本寫死的值。這樣做設定起來更靈活,也更好管理。

  • External IP:

    module "external_global_ip" {
      source = "./modules/global_ip"
    
      name         = var.external_ip_name
      address_type = "EXTERNAL"
      ip_version   = "IPV4"
      labels       = var.external_ip_labels
    }
    
  • Internal IP:

    module "internal_global_ip" {
      source = "./modules/global_ip"
    
      name         = var.internal_ip_name
      address_type = "INTERNAL"
      network      = "default"
      ip_version   = "IPV4"
      purpose      = "PRIVATE_SERVICE_CONNECT"
      address      = var.internal_ip_address
      labels       = var.internal_ip_labels
    }
    

2. 建立全球 IP 模組

建立一個名為 modules/global_ip 的資料夾,然後在裡面加一個 global_ip.tf 檔案。這個檔案會定義一個通用的Global IP 資源。

  • 模組定義 (global_ip.tf)

    variable "name" {
      description = "The name of the global IP"
      type        = string
    }
    
    variable "address_type" {
      description = "The address type (EXTERNAL/INTERNAL)"
      type        = string
    }
    
    variable "ip_version" {
      description = "The IP version (IPV4/IPV6)"
      type        = string
    }
    
    variable "labels" {
      description = "Labels to assign to the IP resource"
      type        = map(string)
    }
    
    resource "google_compute_global_address" "global_ip" {
      name         = var.name
      address_type = var.address_type
      ip_version   = var.ip_version
      labels       = var.labels
    }
    

3. 在 variables.tf 定義變數

在項目的根目錄建立一個 variables.tf 檔案,來定義你模組裡用到的所有變數。

  • 變數定義

    variable "external_ip_name" {
      description = "The name for the external global IP"
      type        = string
    }
    
    variable "external_ip_labels" {
      description = "Labels for the external global IP"
      type        = map(string)
    }
    
    variable "internal_ip_name" {
      description = "The name for the internal global IP"
      type        = string
    }
    
    variable "internal_ip_address" {
      description = "The address for the internal global IP"
      type        = string
    }
    
    variable "internal_ip_labels" {
      description = "Labels for the internal global IP"
      type        = map(string)
    }
    

4. 實施模組化的設定

到你的項目根目錄,然後執行以下 Terraform 指令:

  • 初始化 Terraform:

    terraform init
    
  • 執行 Dry Run:

    terraform plan
    
  • 部署:

    terraform apply
    

通過對 Terraform 設定進行模組化,建立了一個更有組織和可擴展的設定方式。這種方法對於大型項目或管理多個環境時特別有用。

📚References


#terraform #GCP







Related Posts

什麼是法定貨幣?什麼是虛擬貨幣?什麼是安定幣?

什麼是法定貨幣?什麼是虛擬貨幣?什麼是安定幣?

.Net MVC authorization Controller and Workcontext extension in razor view

.Net MVC authorization Controller and Workcontext extension in razor view

1070. Product Sales Analysis III

1070. Product Sales Analysis III


Comments